package com.longrise.android.byjk.plugins.hra.hratabthird;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.hra.HraSearchActivity;
import com.longrise.android.byjk.plugins.hra.hratabfirst.HraOrderEvent;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.common.base.BaseFragment;
import com.longrise.common.utils.PrintLog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Administrator on 2018/1/3.
 * HRA--我的改签
 */

public class MineEndorseFragment extends BaseFragment<MineEndorsePresenter> implements MineEndorseContract.View, MineEndorseAdapter.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {

    public static final String TAG = "MineEndorseFragment";
    public static final int MINEENDORSE = 1;
    public static final int ENDORSEFRAGMENT = 2;
    private BBswipeRefreshLayout mSrl;
    private RecyclerView mRcv;
    private MineEndorseAdapter mAdapter;
    private View mHeadView;
    private Toolbar mToolbar;
    private RelativeLayout mRlToolbarrightiv;
    private ImageView mIvToolbarRightiv;
    private AppBarLayout mine_endorse_title;
    private EntityBean[] mHraListBean;
    private LinearLayout ll_endorse_null;

    @Override
    protected int getLayoutResourse(@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mine_endorse;
    }

    @Override
    public void initView() {
        mine_endorse_title = (AppBarLayout) mRootView.findViewById(R.id.hra_fragment_title);
        mToolbar = (Toolbar) mRootView.findViewById(R.id.byjk_toolbar);
        mSrl = (BBswipeRefreshLayout) mRootView.findViewById(R.id.mine_endorse_sp);
        mRcv = (RecyclerView) mRootView.findViewById(R.id.mine_endorse_rv);
        mRlToolbarrightiv = (RelativeLayout) mRootView.findViewById(R.id.bb_toolbar_right_rl);
        mIvToolbarRightiv = (ImageView) mRootView.findViewById(R.id.iv_toolbar_share);
        ll_endorse_null = (LinearLayout) mRootView.findViewById(R.id.ll_endorse_null);
        EventBus.getDefault().register(this);
        initRcv();
        initEvent();
        mToolbar.setTitle("我的改签");
        setToolbarRightIvTitle(R.drawable.by_ic_search_main);
        mPresenter.getEndorseList();
    }


    private void initRcv() {
//        mHeadView = View.inflate(mContext, R.layout.item_mine_endorse_headview, null);
        mAdapter = new MineEndorseAdapter();
//        mAdapter.addHeaderView(mHeadView);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRcv.setLayoutManager(manager);
        mRcv.setAdapter(mAdapter);
    }

    private void initEvent() {
        mSrl.setOnRefreshListener(this);
        mAdapter.setOnItemClickListener(this);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

    }

    @Override
    public void onClick(EntityBean item) {
        HraOrderEvent event = new HraOrderEvent();
        String endorseId = item.getString("id");
        String timeid = item.getString("timeid");
        String endorse = item.getString("endorse");
        String orderId = item.getString("orderid");
        PrintLog.e(TAG,"endorse=:"+endorse+"---endorseId=:"+endorseId+"---timeId=:"+timeid);
        event.setEndorseId(endorseId);
        event.setTimeId(timeid);
        event.setEndorse(endorse);
        event.setOrderid(orderId);
        EventBus.getDefault().postSticky(event);
        jump2HraEndorse(item);
    }

    //跳转到预约改签界面
    public void jump2HraEndorse(EntityBean item) {
        Intent intent = new Intent(mContext, HraOrderEndorseActivity.class);
        String entityBean = JSONSerializer.getInstance().Serialize(item);
        intent.putExtra(HraOrderEndorseActivity.MINEENDORSEBEAN,entityBean);
        intent.setFlags(ENDORSEFRAGMENT);
        startActivity(intent);
    }

    /**
     * 显示toolbar右边面的iv
     */
    public void setToolbarRightIvTitle(int id) {
        mRlToolbarrightiv.setVisibility(View.VISIBLE);
        mIvToolbarRightiv.setVisibility(View.VISIBLE);
        mIvToolbarRightiv.setImageResource(id);
        mRlToolbarrightiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击toolbar右边的iv,开启搜索界面
                Intent intent = new Intent(mContext,HraSearchActivity.class);
                intent.setFlags(MINEENDORSE);
                startActivity(intent);
            }
        });
    }


    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registRefreshEvent(EndorseRefreshEvent event){
        if(event.isRefresh()){ //刷新界面
            mPresenter.getEndorseList();
        }
    }

    @Override
    public void refreshData(EntityBean bean) {
        mHraListBean = bean.getBeans("result");
        if(mHraListBean != null){
            mAdapter.refreshData(mHraListBean);
            mSrl.setVisibility(View.VISIBLE);
            ll_endorse_null.setVisibility(View.GONE);
        }else {
            ll_endorse_null.setVisibility(View.VISIBLE);
            mSrl.setVisibility(View.GONE);
            mAdapter.setNewData(null);
        }
    }

    @Override
    public void setRefreshing(boolean isRefreshing) {
        mSrl.setRefreshing(isRefreshing);
    }

    @Override
    public void onRefresh() {
        mPresenter.getEndorseList();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
